<!DOCTYPE html>
<html lang="en" class="svg no-js">
<head>
    <meta charset="utf-8" />
    <!--[if IE ]>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
    <![endif]-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="Language" content="en">

    <title>MySQL :: MySQL 8.0 Reference Manual :: 12.12 Bit Functions and Operators</title>
    <link rel="stylesheet" media="all" href="css/main-20190125.min.css" />
            <link rel="stylesheet" media="all" href="css/docs-20190125.min.css" />
    
    
    <link rel="stylesheet" media="print" href="css/print-20190125.min.css" />

    
    
    
    
            <link rel="contents" href="index.html" title="MySQL 8.0 Reference Manual" />
<link rel="start" href="index.html" title="MySQL 8.0 Reference Manual" />
<link rel="prev" href="xml-functions.html" title="12.11 XML Functions" />
<link rel="next" href="encryption-functions.html?ff=nopfpls" title="12.13 Encryption and Compression Functions" />
<link rel="up" href="functions.html" title="12 Functions and Operators" />

    
    <link rel="shortcut icon" href="favicon.ico" />
    <script>(function(H){ H.className=H.className.replace(/\bno-js\b/,'js') })(document.documentElement)</script>
    <script src="js/site-20181120.min.js"></script>

    
    
    
    <!--[if lt IE 9]>
        <script src="https://labs.mysql.com/common/js/polyfills/html5shiv-printshiv-3.7.2.min.js"></script>
        <script src="https://labs.mysql.com/common/js/polyfills/respond-1.4.2.min.js"></script>
    <![endif]-->
        <!--[if IE 9]>
        <style>#docs-sidebar-toc { box-sizing: content-box; }</style>
    <![endif]-->
    </head>

<body class="no-sidebar full-page dev">
<div class="page-wrapper">
    <header>

                        <a href="https://dev.mysql.com/" aria-label="Home" title="MySQL" id="l1-home-link"></a>
        
        <div id="l1-nav-container">
            <div id="l1-line1">
                <div id="l1-auth-links">
                    <a href="https://www.mysql.com/about/contact/"><b>Contact MySQL</b></a>
                    <span id="l1-contact-separator">&nbsp;|&nbsp;</span>
                    <span id="l1-contact-separator-br"><br /></span>
                                             <a href="https://dev.mysql.com/auth/login/?dest=https%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F8.0%2Fen%2Fbit-functions.html">Login</a> &nbsp;|&nbsp;
                         <a href="https://dev.mysql.com/auth/register/">Register</a>
                                    </div>
                <div id="l1-lhs">
                    <div id="l1-slogan">
                        The world's most popular open source database
                    </div>
                    <div id="l1-search-box">
                        <form id="l1-search-form" method="get" action="https://www.oracle.com/search/results">
                        <input type="hidden" name="cat" value="mysql" />
                        <input type="hidden" name="Ntk" value="SI-ALL5" />
                        <input id="l1-search-input" type="search" class="icon-search" placeholder="Search" aria-label="Search" name="Ntt" />
                        </form>
                    </div>
                </div>
            </div>
            <div id="l1-line2">
                <div class="social-icons">
                    <a aria-label="Join us on Facebook" title="Join us on Facebook" href="http://www.facebook.com/mysql"><span class="icon-facebook"></span></a>
                    <a aria-label="Follow us on Twitter" title="Follow us on Twitter" href="https://twitter.com/mysql"><span class="icon-twitter"></span></a>
                    <a aria-label="Follow us on LinkedIn" title="Follow us on LinkedIn" href="https://www.linkedin.com/company/mysql"><span class="icon-linkedin"></span></a>
                    <a aria-label="Visit our YouTube channel" title="Visit our YouTube channel" href="http://www.youtube.com/mysqlchannel"><span class="icon-youtube"></span></a>
                </div>
                <ul id="l1-nav">
                    <li>
                        <a href="https://www.mysql.com/"><!-- <span class="icon-sakila"></span>  -->MySQL.com</a>
                    </li><li>
                        <a href="https://www.mysql.com/downloads/"><!-- <span class="icon-download-thin"></span>  -->Downloads</a>
                    </li><li class="active">
                        <a href="/doc/"><!-- <span class="icon-books"></span>  -->Documentation</a>
                    </li><li>
                        <a href="/"><!-- <span class="icon-code"></span>  -->Developer Zone</a>
                    </li>                </ul>
            </div>
        </div>
        <div id="sub-header">
            <div id="l2-nav-container">
                <div id="l2-nav-toggle">
                    <span class="icon-three-bars"></span>
                </div>
                                                                                        <a class="button nav-button-3" href="/"><span class="icon-code"></span> Developer Zone</a>
                                                                                                                                    <a class="button nav-button-2" href="https://www.mysql.com/downloads/"><span class="icon-download-thin"></span> Downloads</a>
                                                                                                <a class="button nav-button-1" href="https://www.mysql.com/"><span class="icon-sakila"></span> MySQL.com</a>
                                                                        <div id="l2-search-toggle">
                    <span class="icon-search"></span>
                </div>
                <div id="l2-site-icon">
                                                                                                                                                                                                    <span class="icon-books"></span>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        </div>
                <div id="l1-section-title">
                    <span id="l1-section-label">
                        <a href="/doc/">Documentation</a>
                    </span>
                </div>
                <nav>
                    

<ul id="l2-nav">
            	<li class="active"><a class="active " href="/doc/refman/en/">MySQL Server</a>
    			</li>
	        	<li class=""><a  href="/doc/index-enterprise.html">MySQL Enterprise</a>
    			</li>
	        	<li class=""><a  href="/doc/workbench/en/">Workbench</a>
    			</li>
	        	<li class=""><a  href="/doc/en/mysql-innodb-cluster-userguide.html">InnoDB Cluster</a>
    			</li>
	        	<li class=""><a  href="/doc/index-cluster.html">MySQL NDB Cluster</a>
    			</li>
	        	<li class=""><a  href="/doc/index-connectors.html">Connectors</a>
    			</li>
	        	<li class=" last"><a  href="/doc/index-other.html">More</a>
    			</li>
	                        <li class="other-section"><a href="https://www.mysql.com/">MySQL.com</a></li>
                            <li class="other-section"><a href="https://www.mysql.com/downloads/">Downloads</a></li>
                                        <li class="other-section"><a href="/">Developer Zone</a></li>
            </ul>
                </nav>
            </div>
            <div id="l3-search-container"></div>
            <div id="top-orange"><span id="section-nav">Section Menu: &nbsp; </span></div>
        </div>
    </header>
        <div id="page">
                        <div role="main" id="main">

            
                
            
<div>
    <div id="docs-sidebar-toc" class="">
    <div id="docs-toc-inner">
        <div class="docs-sidebar-header" id="docs-nav-header">
            <a class="docs-show-hide-nav" id="docs-hide-nav" href="" title="Hide Sidebar" aria-label="Hide Sidebar"><span class="icon-arrow-small-left"></span></a>
            <div class="docs-nav-links">
                                    
    <a href="xml-functions.html"
        aria-label="Previous" title="Previous: XML Functions"><span
        class="icon-chevron-left"></span></a>
<a href="index.html" aria-label="Start" title="Start"><span class="icon-book-open"></span></a>
        <a aria-label="Up" href="functions.html" title="Up: Functions and Operators"><span class="icon-chevron-up"></span></a>
    <a href="encryption-functions.html" aria-label="Next"
        title="Next: Encryption and Compression Functions"><span
        class="icon-chevron-right"></span></a>
                            </div>
        </div>
                    
<div id="docs-sidebar-search-container">
    <div id="docs-sidebar-search-box">
        <form method="get" action="/mysql-manual/search-page">
            <input type="hidden" name="d" id="d" value="201" />
            <input type="hidden" name="p" id="p" value="1" />
            <input type="text" name="keyword" id="q" title="Search this Manual"
                value=""
                style="color: #bbb;"
                onfocus=""
                onblur="" />

            <button class="docs-sidebar-search-btn" aria-label="Search" title="Search" type="submit">
                <span class="icon-search"></span>
            </button>
        </form>
    </div>
</div>
                <div class="docs-sidebar-nav">
            <a class="docs-icon-home" href="/doc/"><span class="icon-home"></span>Documentation Home</a><hr />
            <div class="docs-sidebar-mtitle">MySQL 8.0 Reference Manual</div>
            <nav class="doctoc" id="doc-201">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="preface.html">Preface and Legal Notices</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="introduction.html">General Information</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="installing.html">Installing and Upgrading MySQL</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="tutorial.html">Tutorial</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="programs.html">MySQL Programs</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="server-administration.html">MySQL Server Administration</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="security.html">Security</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="backup-and-recovery.html">Backup and Recovery</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="optimization.html">Optimization</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="language-structure.html">Language Structure</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="charset.html">Character Sets, Collations, Unicode</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="data-types.html">Data Types</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-down"></span></a></div><div class="docs-sidebar-nav-link"><a href="functions.html">Functions and Operators</a></div></div>                        <div class="docs-submenu">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="func-op-summary-ref.html">Function and Operator Reference</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="type-conversion.html">Type Conversion in Expression Evaluation</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="non-typed-operators.html">Operators</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="operator-precedence.html">Operator Precedence</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="comparison-operators.html">Comparison Functions and Operators</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="logical-operators.html">Logical Operators</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="assignment-operators.html">Assignment Operators</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="control-flow-functions.html">Control Flow Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="string-functions.html">String Functions</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="string-comparison-functions.html">String Comparison Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="regexp.html">Regular Expressions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="string-functions-charset.html">Character Set and Collation of Function Results</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="numeric-functions.html">Numeric Functions and Operators</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="arithmetic-functions.html">Arithmetic Operators</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="mathematical-functions.html">Mathematical Functions</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="date-and-time-functions.html">Date and Time Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="mysql-calendar.html">What Calendar Is Used By MySQL?</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="fulltext-search.html">Full-Text Search Functions</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="fulltext-natural-language.html">Natural Language Full-Text Searches</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="fulltext-boolean.html">Boolean Full-Text Searches</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="fulltext-query-expansion.html">Full-Text Searches with Query Expansion</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="fulltext-stopwords.html">Full-Text Stopwords</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="fulltext-restrictions.html">Full-Text Restrictions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="fulltext-fine-tuning.html">Fine-Tuning MySQL Full-Text Search</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="full-text-adding-collation.html">Adding a Collation for Full-Text Indexing</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="fulltext-search-ngram.html">ngram Full-Text Parser</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="fulltext-search-mecab.html">MeCab Full-Text Parser Plugin</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="cast-functions.html">Cast Functions and Operators</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="xml-functions.html">XML Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link current"><a href="bit-functions.html">Bit Functions and Operators</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="encryption-functions.html">Encryption and Compression Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="locking-functions.html">Locking Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="information-functions.html">Information Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="spatial-analysis-functions.html">Spatial Analysis Functions</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="spatial-function-reference.html">Spatial Function Reference</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="spatial-function-argument-handling.html">Argument Handling by Spatial Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="gis-wkt-functions.html">Functions That Create Geometry Values from WKT Values</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="gis-wkb-functions.html">Functions That Create Geometry Values from WKB Values</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="gis-mysql-specific-functions.html">MySQL-Specific Functions That Create Geometry Values</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="gis-format-conversion-functions.html">Geometry Format Conversion Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="gis-property-functions.html">Geometry Property Functions</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="gis-general-property-functions.html">General Geometry Property Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="gis-point-property-functions.html">Point Property Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="gis-linestring-property-functions.html">LineString and MultiLineString Property Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="gis-polygon-property-functions.html">Polygon and MultiPolygon Property Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="gis-geometrycollection-property-functions.html">GeometryCollection Property Functions</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="spatial-operator-functions.html">Spatial Operator Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="spatial-relation-functions.html">Functions That Test Spatial Relations Between Geometry Objects</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="spatial-relation-functions-object-shapes.html">Spatial Relation Functions That Use Object Shapes</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="spatial-relation-functions-mbr.html">Spatial Relation Functions That Use Minimum Bounding Rectangles</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="spatial-geohash-functions.html">Spatial Geohash Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="spatial-geojson-functions.html">Spatial GeoJSON Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="spatial-convenience-functions.html">Spatial Convenience Functions</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="json-functions.html">JSON Functions</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="json-function-reference.html">JSON Function Reference</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="json-creation-functions.html">Functions That Create JSON Values</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="json-search-functions.html">Functions That Search JSON Values</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="json-modification-functions.html">Functions That Modify JSON Values</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="json-attribute-functions.html">Functions That Return JSON Value Attributes</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="json-table-functions.html">JSON Table Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="json-validation-functions.html">JSON Schema Validation Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="json-utility-functions.html">JSON Utility Functions</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="gtid-functions.html">Functions Used with Global Transaction Identifiers (GTIDs)</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="enterprise-encryption.html">MySQL Enterprise Encryption Functions</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="enterprise-encryption-installation.html">MySQL Enterprise Encryption Installation</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="enterprise-encryption-usage.html">MySQL Enterprise Encryption Usage and Examples</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="enterprise-encryption-function-reference.html">MySQL Enterprise Encryption Function Reference</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="enterprise-encryption-functions.html">MySQL Enterprise Encryption Function Descriptions</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="group-by-functions-and-modifiers.html">Aggregate (GROUP BY) Functions</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="group-by-functions.html">Aggregate (GROUP BY) Function Descriptions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="group-by-modifiers.html">GROUP BY Modifiers</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="group-by-handling.html">MySQL Handling of GROUP BY</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="group-by-functional-dependence.html">Detection of Functional Dependence</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="window-functions.html">Window Functions</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="window-function-descriptions.html">Window Function Descriptions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="window-functions-usage.html">Window Function Concepts and Syntax</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="window-functions-frames.html">Window Function Frame Specification</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="window-functions-named-windows.html">Named Windows</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="window-function-restrictions.html">Window Function Restrictions</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="performance-schema-functions.html">Performance Schema Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="internal-functions.html">Internal Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="miscellaneous-functions.html">Miscellaneous Functions</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable  loaded" aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="precision-math.html">Precision Math</a></div></div>                        <div class="docs-submenu hidden">
                                    
    <ul>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="precision-math-numbers.html">Types of Numeric Values</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="precision-math-decimal-characteristics.html">DECIMAL Data Type Characteristics</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="precision-math-expressions.html">Expression Handling</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="precision-math-rounding.html">Rounding Behavior</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="precision-math-examples.html">Precision Math Examples</a></div></div>                    </li>
        </ul>
                            </div>
                    </li>
        </ul>
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="sql-syntax.html">SQL Statement Syntax</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="data-dictionary.html">MySQL Data Dictionary</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="innodb-storage-engine.html">The InnoDB Storage Engine</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="storage-engines.html">Alternative Storage Engines</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="replication.html">Replication</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="group-replication.html">Group Replication</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="mysql-shell-userguide.html">MySQL Shell</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="document-store.html">Using MySQL as a Document Store</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="mysql-innodb-cluster-userguide.html">InnoDB Cluster</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="mysql-cluster.html">MySQL NDB Cluster 8.0</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="partitioning.html">Partitioning</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="stored-objects.html">Stored Objects</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="information-schema.html">INFORMATION_SCHEMA Tables</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="performance-schema.html">MySQL Performance Schema</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="sys-schema.html">MySQL sys Schema</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="connectors-apis.html">Connectors and APIs</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="extending-mysql.html">Extending MySQL</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="mysql-enterprise.html">MySQL Enterprise Edition</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="workbench.html">MySQL Workbench</a></div></div>                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="faqs.html">MySQL 8.0 Frequently Asked Questions</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="error-handling.html">Errors, Error Codes, and Common Problems</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="restrictions.html">Restrictions and Limits</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><a class="expandable " aria-label="Subnav" href=""><span class="icon-chevron-right"></span></a></div><div class="docs-sidebar-nav-link"><a href="indexes.html">Indexes</a></div></div>                        <div class="docs-submenu hidden">
                            </div>
                    </li>
            <li>
            <div><div class="docs-sidebar-nav-icon"><span class="icon-dot"></span></div><div class="docs-sidebar-nav-link"><a href="glossary.html">MySQL Glossary</a></div></div>                    </li>
        </ul>
                            </nav>
        </div>

        
<a class="docs-sidebar-section" href=""><span class="icon-related"></span>
    <span class="text">Related Documentation</span></a>
<div class="docs-sidebar-accordian open">
    <div class="text">
        <a href="/doc/relnotes/mysql/8.0/en/">MySQL 8.0 Release Notes</a><br />
        <a href="/doc/dev/mysql-server/latest/">MySQL 8.0 Source Code Documentation</a><br />
        </div>
</div>

    <a class="docs-sidebar-section" href=""><span class="icon-download-thin"></span>
        <span class="text">
            Download
                            this Manual
                    </span>
    </a>
    <div class="docs-sidebar-accordian open">
        <div class="text">
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.pdf">PDF (US Ltr)</a>
            - 46.1Mb<br />
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.a4.pdf">PDF (A4)</a>
            - 46.1Mb<br />
                        <a href="https://downloads.mysql.com/docs/mysql-ref-manual-8.0-en-pdf-1-20190611.noarch.rpm">PDF (RPM)</a>
            - 41.5Mb<br />
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.html-chapter.tar.gz">HTML Download (TGZ)</a>
            - 10.6Mb<br />
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.html-chapter.zip">HTML Download (Zip)</a>
            - 10.6Mb<br />
                        <a href="https://downloads.mysql.com/docs/mysql-ref-manual-8.0-en-html-chapter-1-20190611.noarch.rpm">HTML Download (RPM)</a>
            - 9.1Mb<br />
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.man-gpl.tar.gz">Man Pages (TGZ)</a>
            - 220.4Kb<br />
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.man-gpl.zip">Man Pages (Zip)</a>
            - 325.8Kb<br />
                        <a href="https://downloads.mysql.com/docs/mysql-8.0.info.gz">Info (Gzip)</a>
            - 4.1Mb<br />
                        <a href="https://downloads.mysql.com/docs/mysql-8.0.info.zip">Info (Zip)</a>
            - 4.1Mb<br />
                    </div>
    </div>

<a class="docs-sidebar-section" href=""><span class="icon-book"></span>
    <span class="text">Excerpts from this Manual</span></a>
<div class="docs-sidebar-accordian">
    <div class="text">
                <a href="/doc/mysql-backup-excerpt/8.0/en/">MySQL Backup and Recovery</a><br />
                <a href="/doc/mysql-g11n-excerpt/8.0/en/">MySQL Globalization</a><br />
                <a href="/doc/mysql-infoschema-excerpt/8.0/en/">MySQL Information Schema</a><br />
                <a href="/doc/mysql-installation-excerpt/8.0/en/">MySQL Installation Guide</a><br />
                <a href="/doc/mysql-security-excerpt/8.0/en/">Security in MySQL</a><br />
                <a href="/doc/mysql-startstop-excerpt/8.0/en/">Starting and Stopping MySQL</a><br />
                <a href="/doc/mysql-linuxunix-excerpt/8.0/en/">MySQL and Linux/Unix</a><br />
                <a href="/doc/mysql-windows-excerpt/8.0/en/">MySQL and Windows</a><br />
                <a href="/doc/mysql-osx-excerpt/8.0/en/">MySQL and OS X</a><br />
                <a href="/doc/mysql-solaris-excerpt/8.0/en/">MySQL and Solaris</a><br />
                <a href="/doc/mysql-sourcebuild-excerpt/8.0/en/">Building MySQL from Source</a><br />
                <a href="/doc/mysql-reslimits-excerpt/8.0/en/">MySQL Restrictions and Limitations</a><br />
                <a href="/doc/mysql-partitioning-excerpt/8.0/en/">MySQL Partitioning</a><br />
                <a href="/doc/mysql-secure-deployment-guide/8.0/en/">MySQL Secure Deployment Guide</a><br />
                <a href="/doc/mysql-tutorial-excerpt/8.0/en/">MySQL Tutorial</a><br />
                <a href="/doc/mysql-perfschema-excerpt/8.0/en/">MySQL Performance Schema</a><br />
                <a href="/doc/mysql-replication-excerpt/8.0/en/">MySQL Replication</a><br />
                <a href="/doc/mysql-repo-excerpt/8.0/en/">Using the MySQL Yum Repository</a><br />
            </div>
</div>

        <br /><span id="wkr"><br /></span>
    </div>
    </div>

    <div id="docs-main" class="has-toc">
    <div id="docs-main-inner">

                <div class="right" id="docs-version-nav">
            <a href="" id="docs-version-nav-toggle">version 8.0
            <span class="icon-chevron-down"></span></a>
            <div id="docs-version-list">
                                                                                            <a                         href="/doc/refman/5.7/en/bit-functions.html">
                        5.7
                                            </a><br />
                                                                            <a                         href="/doc/refman/5.6/en/bit-functions.html">
                        5.6
                                            </a><br />
                                                                            <a                         href="/doc/refman/5.5/en/bit-functions.html">
                        5.5
                                            </a><br />
                                                                        <div id="docs-version-nav-lang">
                                                                                                                                                    <a                                     href="/doc/refman/5.6/ja/bit-functions.html">
                                    5.6&nbsp;
                                                                            Japanese
                                                                    </a><br />
                                                                                            </div>
                            </div>
        </div>
        
        <div id="docs-show-nav" class="left hidden" style="margin-right: 15px;">
            <a class="docs-show-hide-nav" href="" aria-label="Show Sidebar"
                title="Show Sidebar"><span class="icon-arrow-small-right"></span></a>
        </div>

                        <div id="docs-breadcrumbs">
            <a href="/doc/refman/8.0/en/">MySQL 8.0 Reference Manual</a> &nbsp;/&nbsp;
                                                        <a href="functions.html">Functions and Operators</a> &nbsp;/&nbsp;
                                                                                    Bit Functions and Operators
                                    </div>
        
        <div id="docs-body">
        
<div class="section">

<div class="titlepage">
<div>
<div>
<h2 class="title"><a name="bit-functions"></a>12.12 Bit Functions and Operators</h2>

</div>

</div>

</div>
<a class="indexterm" name="idm139663188630752"></a><a class="indexterm" name="idm139663188629296"></a><a class="indexterm" name="idm139663188627808"></a><a class="indexterm" name="idm139663188626736"></a><a class="indexterm" name="idm139663188625664"></a><a class="indexterm" name="idm139663188624160"></a><a class="indexterm" name="idm139663188622704"></a><a class="indexterm" name="idm139663188621632"></a>
<div class="table">
<a name="idm139663188620560"></a><p class="title"><b>Table 12.16 Bit Functions and Operators</b></p>
<div class="table-contents">
<table frame="box" rules="all" summary="A reference that lists bit functions and operators."><col width="40%"><col width="60%"><thead><tr><th scope="col">Name</th>
<th scope="col">Description</th>
</tr></thead><tbody><tr><td scope="row"><a class="link" href="bit-functions.html#function_bit-count"><code class="literal">BIT_COUNT()</code></a></td>
<td>
      Return the number of bits that are set
    </td>
</tr><tr><td scope="row"><a class="link" href="bit-functions.html#operator_bitwise-and"><code class="literal">&amp;</code></a></td>
<td>
      Bitwise AND
    </td>
</tr><tr><td scope="row"><a class="link" href="bit-functions.html#operator_bitwise-invert"><code class="literal">~</code></a></td>
<td>
      Bitwise inversion
    </td>
</tr><tr><td scope="row"><a class="link" href="bit-functions.html#operator_bitwise-or"><code class="literal">|</code></a></td>
<td>
      Bitwise OR
    </td>
</tr><tr><td scope="row"><a class="link" href="bit-functions.html#operator_bitwise-xor"><code class="literal">^</code></a></td>
<td>
      Bitwise XOR
    </td>
</tr><tr><td scope="row"><a class="link" href="bit-functions.html#operator_left-shift"><code class="literal">&lt;&lt;</code></a></td>
<td>
      Left shift
    </td>
</tr><tr><td scope="row"><a class="link" href="bit-functions.html#operator_right-shift"><code class="literal">&gt;&gt;</code></a></td>
<td>
      Right shift
    </td>
</tr></tbody></table>
</div>

</div>
<br class="table-break"><p>
      Bit functions and operators comprise
      <a class="link" href="bit-functions.html#function_bit-count"><code class="literal">BIT_COUNT()</code></a>,
      <a class="link" href="group-by-functions.html#function_bit-and"><code class="literal">BIT_AND()</code></a>,
      <a class="link" href="group-by-functions.html#function_bit-or"><code class="literal">BIT_OR()</code></a>,
      <a class="link" href="group-by-functions.html#function_bit-xor"><code class="literal">BIT_XOR()</code></a>,
      <a class="link" href="bit-functions.html#operator_bitwise-and"><code class="literal">&amp;</code></a>,
      <a class="link" href="bit-functions.html#operator_bitwise-or"><code class="literal">|</code></a>,
      <a class="link" href="bit-functions.html#operator_bitwise-xor"><code class="literal">^</code></a>,
      <a class="link" href="bit-functions.html#operator_bitwise-invert"><code class="literal">~</code></a>,
      <a class="link" href="bit-functions.html#operator_left-shift"><code class="literal">&lt;&lt;</code></a>, and
      <a class="link" href="bit-functions.html#operator_right-shift"><code class="literal">&gt;&gt;</code></a>.
      (The <a class="link" href="group-by-functions.html#function_bit-and"><code class="literal">BIT_AND()</code></a>,
      <a class="link" href="group-by-functions.html#function_bit-or"><code class="literal">BIT_OR()</code></a>, and
      <a class="link" href="group-by-functions.html#function_bit-xor"><code class="literal">BIT_XOR()</code></a> aggregate functions are
      described in <a class="xref" href="group-by-functions.html" title="12.20.1 Aggregate (GROUP BY) Function Descriptions">Section 12.20.1, “Aggregate (GROUP BY) Function Descriptions”</a>.) Prior to MySQL
      8.0, bit functions and operators required
      <a class="link" href="integer-types.html" title="11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT"><code class="literal">BIGINT</code></a> (64-bit integer) arguments
      and returned <a class="link" href="integer-types.html" title="11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT"><code class="literal">BIGINT</code></a> values, so they
      had a maximum range of 64 bits.
      Non-<a class="link" href="integer-types.html" title="11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT"><code class="literal">BIGINT</code></a> arguments were converted
      to <a class="link" href="integer-types.html" title="11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT"><code class="literal">BIGINT</code></a> prior to performing the
      operation and truncation could occur.
    </p><p>
      In MySQL 8.0, bit functions and operators permit
      binary string type arguments
      (<a class="link" href="binary-varbinary.html" title="11.4.2 The BINARY and VARBINARY Types"><code class="literal">BINARY</code></a>,
      <a class="link" href="binary-varbinary.html" title="11.4.2 The BINARY and VARBINARY Types"><code class="literal">VARBINARY</code></a>, and the
      <a class="link" href="blob.html" title="11.4.3 The BLOB and TEXT Types"><code class="literal">BLOB</code></a> types) and return a value of
      like type, which enables them to take arguments and produce return
      values larger than 64 bits. Nonbinary string arguments are
      converted to <a class="link" href="integer-types.html" title="11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT"><code class="literal">BIGINT</code></a> and processed
      as such, as before.
    </p><p>
      An implication of this change in behavior is that bit operations
      on binary string arguments might produce a different result in
      MySQL 8.0 than in 5.7. For information
      about how to prepare in MySQL 5.7 for potential
      incompatibilities between MySQL 5.7 and 8.0, see
      <a class="ulink" href="https://dev.mysql.com/doc/refman/5.7/en/bit-functions.html" target="_top">Bit Functions and Operators</a>, in
      <a class="ulink" href="https://dev.mysql.com/doc/refman/5.7/en/" target="_top">MySQL 5.7 Reference Manual</a>.
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><a class="xref" href="bit-functions.html#bit-operations-5-7" title="Bit Operations Prior to MySQL 8.0">Bit Operations Prior to MySQL 8.0</a></p></li><li class="listitem"><p><a class="xref" href="bit-functions.html#bit-operations-8-0" title="Bit Operations in MySQL 8.0">Bit Operations in MySQL 8.0</a></p></li><li class="listitem"><p><a class="xref" href="bit-functions.html#bit-operations-binary-string-examples" title="Binary String Bit-Operation Examples">Binary String Bit-Operation Examples</a></p></li><li class="listitem"><p><a class="xref" href="bit-functions.html#bit-operations-and-or-xor" title="Bitwise AND, OR, and XOR Operations">Bitwise AND, OR, and XOR Operations</a></p></li><li class="listitem"><p><a class="xref" href="bit-functions.html#bit-operations-complement-shift" title="Bitwise Complement and Shift Operations">Bitwise Complement and Shift Operations</a></p></li><li class="listitem"><p><a class="xref" href="bit-functions.html#bit-operations-bit-count" title="BIT_COUNT() Operations">BIT_COUNT() Operations</a></p></li><li class="listitem"><p><a class="xref" href="bit-functions.html#bit-operations-bit-aggregate" title="BIT_AND(), BIT_OR(), and BIT_XOR() Operations">BIT_AND(), BIT_OR(), and BIT_XOR() Operations</a></p></li><li class="listitem"><p><a class="xref" href="bit-functions.html#bit-operations-literal-handling" title="Special Handling of Hexadecimal Literals, Bit Literals, and NULL Literals">Special Handling of Hexadecimal Literals, Bit Literals, and NULL
Literals</a></p></li><li class="listitem"><p><a class="xref" href="bit-functions.html#bit-operations-incompatibilities" title="Bit-Operation Incompatibilities with MySQL 5.7">Bit-Operation Incompatibilities with MySQL 5.7</a></p></li></ul>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="bit-operations-5-7"></a>Bit Operations Prior to MySQL 8.0</h3>

</div>

</div>

</div>
<p>
        Bit operations prior to MySQL 8.0 handle only unsigned 64-bit
        integer argument and result values (that is, unsigned
        <a class="link" href="integer-types.html" title="11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT"><code class="literal">BIGINT</code></a> values). Conversion of
        arguments of other types to
        <a class="link" href="integer-types.html" title="11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT"><code class="literal">BIGINT</code></a> occurs as necessary.
        Examples:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            This statement operates on numeric literals, treated as
            unsigned 64-bit integers:
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT 127 | 128, 128 &lt;&lt; 2, BIT_COUNT(15);
+-----------+----------+---------------+
| 127 | 128 | 128 &lt;&lt; 2 | BIT_COUNT(15) |
+-----------+----------+---------------+
|       255 |      512 |             4 |
+-----------+----------+---------------+</code></pre></li><li class="listitem"><p>
            This statement performs to-number conversions on the string
            arguments (<code class="literal">'127'</code> to
            <code class="literal">127</code>, and so forth) before performing the
            same operations as the first statement and producing the
            same results:
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT '127' | '128', '128' &lt;&lt; 2, BIT_COUNT('15');
+---------------+------------+-----------------+
| '127' | '128' | '128' &lt;&lt; 2 | BIT_COUNT('15') |
+---------------+------------+-----------------+
|           255 |        512 |               4 |
+---------------+------------+-----------------+</code></pre></li><li class="listitem"><p>
            This statement uses hexadecimal literals for the
            bit-operation arguments. MySQL by default treats hexadecimal
            literals as binary strings, but in numeric context evaluates
            them as numbers (see
            <a class="xref" href="hexadecimal-literals.html" title="9.1.4 Hexadecimal Literals">Section 9.1.4, “Hexadecimal Literals”</a>). Prior to MySQL 8.0,
            numeric context includes bit operations. Examples:
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT X'7F' | X'80', X'80' &lt;&lt; 2, BIT_COUNT(X'0F');
+---------------+------------+------------------+
| X'7F' | X'80' | X'80' &lt;&lt; 2 | BIT_COUNT(X'0F') |
+---------------+------------+------------------+
|           255 |        512 |                4 |
+---------------+------------+------------------+</code></pre><p>
            Handling of bit-value literals in bit operations is similar
            to hexadecimal literals (that is, as numbers).
</p></li></ul>
</div>

</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="bit-operations-8-0"></a>Bit Operations in MySQL 8.0</h3>

</div>

</div>

</div>
<p>
        MySQL 8.0 extends bit operations to handle binary string
        arguments directly (without conversion) and produce binary
        string results. (Arguments that are not integers or binary
        strings are still converted to integers, as before.) This
        extension enhances bit operations in the following ways:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Bit operations become possible on values longer than 64
            bits.
          </p></li><li class="listitem"><p>
            It is easier to perform bit operations on values that are
            more naturally represented as binary strings than as
            integers.
</p></li></ul>
</div>
<p>
        For example, consider UUID values and IPv6 addresses, which have
        human-readable text formats like this:
      </p><pre class="programlisting copytoclipboard line-numbers language-none"><code class="language-none">UUID: 6ccd780c-baba-1026-9564-5b8c656024db
IPv6: fe80::219:d1ff:fe91:1a72</code></pre><p>
        It is cumbersome to operate on text strings in those formats. An
        alternative is convert them to fixed-length binary strings
        without delimiters. <a class="link" href="miscellaneous-functions.html#function_uuid-to-bin"><code class="literal">UUID_TO_BIN()</code></a>
        and <a class="link" href="miscellaneous-functions.html#function_inet6-aton"><code class="literal">INET6_ATON()</code></a> each produce a
        value of data type <a class="link" href="binary-varbinary.html" title="11.4.2 The BINARY and VARBINARY Types"><code class="literal">BINARY(16)</code></a>, a
        binary string 16 bytes (128 bits) long. The following statements
        illustrate this (<code class="literal">HEX()</code> is used to produce
        displayable values):
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT HEX(UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db'));
+----------------------------------------------------------+
| HEX(UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db')) |
+----------------------------------------------------------+
| 6CCD780CBABA102695645B8C656024DB                         |
+----------------------------------------------------------+
mysql&gt; SELECT HEX(INET6_ATON('fe80::219:d1ff:fe91:1a72'));
+---------------------------------------------+
| HEX(INET6_ATON('fe80::219:d1ff:fe91:1a72')) |
+---------------------------------------------+
| FE800000000000000219D1FFFE911A72            |
+---------------------------------------------+</code></pre><p>
        Those binary values are easily manipulable with bit operations
        to perform actions such as extracting the timestamp from UUID
        values, or extracting the network and host parts of IPv6
        addresses. (For examples, see later in this discussion.)
      </p><p>
        Arguments that count as binary strings include column values,
        routine parameters, local variables, and user-defined variables
        that have a binary string type:
        <a class="link" href="binary-varbinary.html" title="11.4.2 The BINARY and VARBINARY Types"><code class="literal">BINARY</code></a>,
        <a class="link" href="binary-varbinary.html" title="11.4.2 The BINARY and VARBINARY Types"><code class="literal">VARBINARY</code></a>, or one of the
        <a class="link" href="blob.html" title="11.4.3 The BLOB and TEXT Types"><code class="literal">BLOB</code></a> types.
      </p><p>
        What about hexadecimal literals and bit literals? Recall that
        those are binary strings by default in MySQL, but numbers in
        numeric context. How are they handled for bit operations in
        MySQL 8.0? Does MySQL continue to evaluate them in numeric
        context, as is done prior to MySQL 8.0? Or do bit operations
        evaluate them as binary strings, now that binary strings can be
        handled <span class="quote">“<span class="quote">natively</span>”</span> without conversion?
      </p><p>
        Answer: It has been common to specify arguments to bit
        operations using hexadecimal literals or bit literals with the
        intent that they represent numbers, so MySQL continues to
        evaluate bit operations in numeric context when all bit
        arguments are hexadecimal or bit literals, for backward
        compatility. If you require evaluation as binary strings
        instead, that is easily accomplished: Use the
        <code class="literal">_binary</code> introducer for at least one literal.
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            These bit operations evaluate the hexadecimal literals and
            bit literals as integers:
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT X'40' | X'01', b'11110001' &amp; b'01001111';
+---------------+---------------------------+
| X'40' | X'01' | b'11110001' &amp; b'01001111' |
+---------------+---------------------------+
|            65 |                        65 |
+---------------+---------------------------+</code></pre></li><li class="listitem"><p>
            These bit operations evaluate the hexadecimal literals and
            bit literals as binary strings, due to the
            <code class="literal">_binary</code> introducer:
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT _binary X'40' | X'01', b'11110001' &amp; _binary b'01001111';
+-----------------------+-----------------------------------+
| _binary X'40' | X'01' | b'11110001' &amp; _binary b'01001111' |
+-----------------------+-----------------------------------+
| A                     | A                                 |
+-----------------------+-----------------------------------+</code></pre></li></ul>
</div>
<p>
        Although the bit operations in both statements produce a result
        with a numeric value of 65, the second statement operates in
        binary-string context, for which 65 is ASCII
        <code class="literal">A</code>.
      </p><p>
        In numeric evaluation context, permitted values of hexadecimal
        literal and bit literal arguments have a maximum of 64 bits, as
        do results. By contrast, in binary-string evaluation context,
        permitted arguments (and results) can exceed 64 bits:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT _binary X'4040404040404040' | X'0102030405060708';
+---------------------------------------------------+
| _binary X'4040404040404040' | X'0102030405060708' |
+---------------------------------------------------+
| ABCDEFGH                                          |
+---------------------------------------------------+</code></pre><p>
        There are several ways to refer to a hexadecimal literal or bit
        literal in a bit operation to cause binary-string evaluation:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">_binary <em class="replaceable">literal</em>
BINARY <em class="replaceable">literal</em>
CAST(<em class="replaceable">literal</em> AS BINARY)</code></pre><p>
        Another way to produce binary-string evaluation of hexadecimal
        literals or bit literals is to assign them to user-defined
        variables, which results in variables that have a binary string
        type:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SET @v1 = X'40', @v2 = X'01', @v3 = b'11110001', @v4 = b'01001111';
mysql&gt; SELECT @v1 | @v2, @v3 &amp; @v4;
+-----------+-----------+
| @v1 | @v2 | @v3 &amp; @v4 |
+-----------+-----------+
| A         | A         |
+-----------+-----------+</code></pre><p>
        In binary-string context, bitwise operation arguments must have
        the same length or an
        <a class="link" href="server-error-reference.html#error_er_invalid_bitwise_operands_size"><code class="literal">ER_INVALID_BITWISE_OPERANDS_SIZE</code></a>
        error occurs:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT _binary X'40' | X'0001';
ERROR 3513 (HY000): Binary operands of bitwise
operators must be of equal length</code></pre><p>
        To satisfy the equal-length requirement, pad the shorter value
        with leading zero digits or, if the longer value begins with
        leading zero digits and a shorter result value is acceptable,
        strip them:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT _binary X'0040' | X'0001';
+---------------------------+
| _binary X'0040' | X'0001' |
+---------------------------+
|  A                        |
+---------------------------+
mysql&gt; SELECT _binary X'40' | X'01';
+-----------------------+
| _binary X'40' | X'01' |
+-----------------------+
| A                     |
+-----------------------+</code></pre><p>
        Padding or stripping can also be accomplished using functions
        such as <a class="link" href="string-functions.html#function_lpad"><code class="literal">LPAD()</code></a>,
        <a class="link" href="string-functions.html#function_rpad"><code class="literal">RPAD()</code></a>,
        <a class="link" href="string-functions.html#function_substr"><code class="literal">SUBSTR()</code></a>, or
        <a class="link" href="cast-functions.html#function_cast"><code class="literal">CAST()</code></a>. In such cases, the
        expression arguments are no longer all literals and
        <code class="literal">_binary</code> becomes unnecessary. Examples:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT LPAD(X'40', 2, X'00') | X'0001';
+---------------------------------+
| LPAD(X'40', 2, X'00') | X'0001' |
+---------------------------------+
|  A                              |
+---------------------------------+
mysql&gt; SELECT X'40' | SUBSTR(X'0001', 2, 1);
+-------------------------------+
| X'40' | SUBSTR(X'0001', 2, 1) |
+-------------------------------+
| A                             |
+-------------------------------+</code></pre>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="bit-operations-binary-string-examples"></a>Binary String Bit-Operation Examples</h3>

</div>

</div>

</div>
<p>
        The following example illustrates use of bit operations to
        extract parts of a UUID value, in this case, the timestamp and
        IEEE 802 node number. This technique requires bitmasks for each
        extracted part.
      </p><p>
        Convert the text UUID to the corresponding 16-byte binary value
        so that it can be manipulated using bit operations in
        binary-string context:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SET @uuid = UUID_TO_BIN('6ccd780c-baba-1026-9564-5b8c656024db');
mysql&gt; SELECT HEX(@uuid);
+----------------------------------+
| HEX(@uuid)                       |
+----------------------------------+
| 6CCD780CBABA102695645B8C656024DB |
+----------------------------------+</code></pre><p>
        Construct bitmasks for the timestamp and node number parts of
        the value. The timestamp comprises the first three parts (64
        bits, bits 0 to 63) and the node number is the last part (48
        bits, bits 80 to 127):
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SET @ts_mask = CAST(X'FFFFFFFFFFFFFFFF' AS BINARY(16));
mysql&gt; SET @node_mask = CAST(X'FFFFFFFFFFFF' AS BINARY(16)) &gt;&gt; 80;
mysql&gt; SELECT HEX(@ts_mask);
+----------------------------------+
| HEX(@ts_mask)                    |
+----------------------------------+
| FFFFFFFFFFFFFFFF0000000000000000 |
+----------------------------------+
mysql&gt; SELECT HEX(@node_mask);
+----------------------------------+
| HEX(@node_mask)                  |
+----------------------------------+
| 00000000000000000000FFFFFFFFFFFF |
+----------------------------------+</code></pre><p>
        The <code class="literal">CAST(... AS BINARY(16))</code> function is used
        here because the masks must be the same length as the UUID value
        against which they are applied. The same result can be produced
        using other functions to pad the masks to the required length:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">SET @ts_mask= RPAD(X'FFFFFFFFFFFFFFFF' , 16, X'00');
SET @node_mask = LPAD(X'FFFFFFFFFFFF', 16, X'00') ;</code></pre><p>
        Use the masks to extract the timestamp and node number parts:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT HEX(@uuid &amp; @ts_mask) AS 'timestamp part';
+----------------------------------+
| timestamp part                   |
+----------------------------------+
| 6CCD780CBABA10260000000000000000 |
+----------------------------------+
mysql&gt; SELECT HEX(@uuid &amp; @node_mask) AS 'node part';
+----------------------------------+
| node part                        |
+----------------------------------+
| 000000000000000000005B8C656024DB |
+----------------------------------+</code></pre><p>
        The preceding example uses these bit operations: right shift
        (<a class="link" href="bit-functions.html#operator_right-shift"><code class="literal">&gt;&gt;</code></a>)
        and bitwise AND
        (<a class="link" href="bit-functions.html#operator_bitwise-and"><code class="literal">&amp;</code></a>).
</p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">

<div class="admon-title">
Note
</div>
<p>
          <a class="link" href="miscellaneous-functions.html#function_uuid-to-bin"><code class="literal">UUID_TO_BIN()</code></a> takes a flag that
          causes some bit rearrangement in the resulting binary UUID
          value. If you use that flag, modify the extraction masks
          accordingly.
</p>
</div>
<p>
        The next example uses bit operations to extract the network and
        host parts of an IPv6 address. Suppose that the network part has
        a length of 80 bits. Then the host part has a length of 128
        − 80 = 48 bits. To extract the network and host parts of
        the address, convert it to a binary string, then use bit
        operations in binary-string context.
      </p><p>
        Convert the text IPv6 address to the corresponding binary
        string:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql one-line"><code class="language-sql">mysql&gt; SET @ip = INET6_ATON('fe80::219:d1ff:fe91:1a72');</code></pre><p>
        Define the network length in bits:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql one-line"><code class="language-sql">mysql&gt; SET @net_len = 80;</code></pre><p>
        Construct network and host masks by shifting the all-ones
        address left or right. To do this, begin with the address
        <code class="literal">::</code>, which is shorthand for all zeros, as you
        can see by converting it to a binary string like this:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT HEX(INET6_ATON('::')) AS 'all zeros';
+----------------------------------+
| all zeros                        |
+----------------------------------+
| 00000000000000000000000000000000 |
+----------------------------------+</code></pre><p>
        To produce the complementary value (all ones), use the
        <a class="link" href="bit-functions.html#operator_bitwise-invert"><code class="literal">~</code></a>
        operator to invert the bits:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT HEX(~INET6_ATON('::')) AS 'all ones';
+----------------------------------+
| all ones                         |
+----------------------------------+
| FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF |
+----------------------------------+</code></pre><p>
        Shift the all-ones value left or right to produce the network
        and host masks:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SET @net_mask = ~INET6_ATON('::') &lt;&lt; (128 - @net_len);
mysql&gt; SET @host_mask = ~INET6_ATON('::') &gt;&gt; @net_len;</code></pre><p>
        Display the masks to verify that they cover the correct parts of
        the address:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT INET6_NTOA(@net_mask) AS 'network mask';
+----------------------------+
| network mask               |
+----------------------------+
| ffff:ffff:ffff:ffff:ffff:: |
+----------------------------+
mysql&gt; SELECT INET6_NTOA(@host_mask) AS 'host mask';
+------------------------+
| host mask              |
+------------------------+
| ::ffff:255.255.255.255 |
+------------------------+</code></pre><p>
        Extract and display the network and host parts of the address:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SET @net_part = @ip &amp; @net_mask;
mysql&gt; SET @host_part = @ip &amp; @host_mask;
mysql&gt; SELECT INET6_NTOA(@net_part) AS 'network part';
+-----------------+
| network part    |
+-----------------+
| fe80::219:0:0:0 |
+-----------------+
mysql&gt; SELECT INET6_NTOA(@host_part) AS 'host part';
+------------------+
| host part        |
+------------------+
| ::d1ff:fe91:1a72 |
+------------------+</code></pre><p>
        The preceding example uses these bit operations: Complement
        (<a class="link" href="bit-functions.html#operator_bitwise-invert"><code class="literal">~</code></a>),
        left shift
        (<a class="link" href="bit-functions.html#operator_left-shift"><code class="literal">&lt;&lt;</code></a>),
        and bitwise AND
        (<a class="link" href="bit-functions.html#operator_bitwise-and"><code class="literal">&amp;</code></a>).
      </p><p>
        The remaining discussion provides details on argument handling
        for each group of bit operations, more information about
        literal-value handling in bit operations, and potential
        incompatibilities between MySQL 8.0 and older MySQL versions.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="bit-operations-and-or-xor"></a>Bitwise AND, OR, and XOR Operations</h3>

</div>

</div>

</div>
<p>
        For <a class="link" href="bit-functions.html#operator_bitwise-and"><code class="literal">&amp;</code></a>,
        <a class="link" href="bit-functions.html#operator_bitwise-or"><code class="literal">|</code></a>, and
        <a class="link" href="bit-functions.html#operator_bitwise-xor"><code class="literal">^</code></a> bit
        operations, the result type depends on whether the arguments are
        evaluated as binary strings or numbers:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Binary-string evaluation occurs when the arguments have a
            binary string type, and at least one of them is not a
            hexadecimal literal, bit literal, or <code class="literal">NULL</code>
            literal. Numeric evaluation occurs otherwise, with argument
            conversion to unsigned 64-bit integers as necessary.
          </p></li><li class="listitem"><p>
            Binary-string evaluation produces a binary string of the
            same length as the arguments. If the arguments have unequal
            lengths, an
            <a class="link" href="server-error-reference.html#error_er_invalid_bitwise_operands_size"><code class="literal">ER_INVALID_BITWISE_OPERANDS_SIZE</code></a>
            error occurs. Numeric evaluation produces an unsigned 64-bit
            integer.
</p></li></ul>
</div>
<p>
        Examples of numeric evaluation:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT 64 | 1, X'40' | X'01';
+--------+---------------+
| 64 | 1 | X'40' | X'01' |
+--------+---------------+
|     65 |            65 |
+--------+---------------+</code></pre><p>
        Examples of binary-string evaluation:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT _binary X'40' | X'01';
+-----------------------+
| _binary X'40' | X'01' |
+-----------------------+
| A                     |
+-----------------------+
mysql&gt; SET @var1 = X'40', @var2 = X'01';
mysql&gt; SELECT @var1 | @var2;
+---------------+
| @var1 | @var2 |
+---------------+
| A             |
+---------------+</code></pre>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="bit-operations-complement-shift"></a>Bitwise Complement and Shift Operations</h3>

</div>

</div>

</div>
<p>
        For <a class="link" href="bit-functions.html#operator_bitwise-invert"><code class="literal">~</code></a>,
        <a class="link" href="bit-functions.html#operator_left-shift"><code class="literal">&lt;&lt;</code></a>,
        and
        <a class="link" href="bit-functions.html#operator_right-shift"><code class="literal">&gt;&gt;</code></a>
        bit operations, the result type depends on whether the bit
        argument is evaluated as a binary string or number:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Binary-string evaluation occurs when the bit argument has a
            binary string type, and is not a hexadecimal literal, bit
            literal, or <code class="literal">NULL</code> literal. Numeric
            evaluation occurs otherwise, with argument conversion to an
            unsigned 64-bit integer as necessary.
          </p></li><li class="listitem"><p>
            Binary-string evaluation produces a binary string of the
            same length as the bit argument. Numeric evaluation produces
            an unsigned 64-bit integer.
</p></li></ul>
</div>
<p>
        For shift operations, bits shifted off the end of the value are
        lost without warning, regardless of the argument type. In
        particular, if the shift count is greater or equal to the number
        of bits in the bit argument, all bits in the result are 0.
      </p><p>
        Examples of numeric evaluation:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT ~0, 64 &lt;&lt; 2, X'40' &lt;&lt; 2;
+----------------------+---------+------------+
| ~0                   | 64 &lt;&lt; 2 | X'40' &lt;&lt; 2 |
+----------------------+---------+------------+
| 18446744073709551615 |     256 |        256 |
+----------------------+---------+------------+</code></pre><p>
        Examples of binary-string evaluation:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT HEX(_binary X'1111000022220000' &gt;&gt; 16);
+----------------------------------------+
| HEX(_binary X'1111000022220000' &gt;&gt; 16) |
+----------------------------------------+
| 0000111100002222                       |
+----------------------------------------+
mysql&gt; SELECT HEX(_binary X'1111000022220000' &lt;&lt; 16);
+----------------------------------------+
| HEX(_binary X'1111000022220000' &lt;&lt; 16) |
+----------------------------------------+
| 0000222200000000                       |
+----------------------------------------+
mysql&gt; SET @var1 = X'F0F0F0F0';
mysql&gt; SELECT HEX(~@var1);
+-------------+
| HEX(~@var1) |
+-------------+
| 0F0F0F0F    |
+-------------+</code></pre>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="bit-operations-bit-count"></a>BIT_COUNT() Operations</h3>

</div>

</div>

</div>
<p>
        The <a class="link" href="bit-functions.html#function_bit-count"><code class="literal">BIT_COUNT()</code></a> function always
        returns an unsigned 64-bit integer, or <code class="literal">NULL</code>
        if the argument is <code class="literal">NULL</code>.
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT BIT_COUNT(127);
+----------------+
| BIT_COUNT(127) |
+----------------+
|              7 |
+----------------+
mysql&gt; SELECT BIT_COUNT(b'010101'), BIT_COUNT(_binary b'010101');
+----------------------+------------------------------+
| BIT_COUNT(b'010101') | BIT_COUNT(_binary b'010101') |
+----------------------+------------------------------+
|                    3 |                            3 |
+----------------------+------------------------------+</code></pre>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="bit-operations-bit-aggregate"></a>BIT_AND(), BIT_OR(), and BIT_XOR() Operations</h3>

</div>

</div>

</div>
<p>
        For the <a class="link" href="group-by-functions.html#function_bit-and"><code class="literal">BIT_AND()</code></a>,
        <a class="link" href="group-by-functions.html#function_bit-or"><code class="literal">BIT_OR()</code></a>, and
        <a class="link" href="group-by-functions.html#function_bit-xor"><code class="literal">BIT_XOR()</code></a> bit functions, the
        result type depends on whether the function argument values are
        evaluated as binary strings or numbers:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            Binary-string evaluation occurs when the argument values
            have a binary string type, and the argument is not a
            hexadecimal literal, bit literal, or <code class="literal">NULL</code>
            literal. Numeric evaluation occurs otherwise, with argument
            value conversion to unsigned 64-bit integers as necessary.
          </p></li><li class="listitem"><p>
            Binary-string evaluation produces a binary string of the
            same length as the argument values. If argument values have
            unequal lengths, an
            <a class="link" href="server-error-reference.html#error_er_invalid_bitwise_operands_size"><code class="literal">ER_INVALID_BITWISE_OPERANDS_SIZE</code></a>
            error occurs. If the argument size exceeds 511 bytes, an
            <a class="link" href="server-error-reference.html#error_er_invalid_bitwise_aggregate_operands_size"><code class="literal">ER_INVALID_BITWISE_AGGREGATE_OPERANDS_SIZE</code></a>
            error occurs. Numeric evaluation produces an unsigned 64-bit
            integer.
</p></li></ul>
</div>
<p>
        <code class="literal">NULL</code> values do not affect the result unless
        all values are <code class="literal">NULL</code>. In that case, the result
        is a neutral value having the same length as the length of the
        argument values (all bits 1 for
        <a class="link" href="group-by-functions.html#function_bit-and"><code class="literal">BIT_AND()</code></a>, all bits 0 for
        <a class="link" href="group-by-functions.html#function_bit-or"><code class="literal">BIT_OR()</code></a>, and
        <a class="link" href="group-by-functions.html#function_bit-xor"><code class="literal">BIT_XOR()</code></a>).
      </p><p>
        Example:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; CREATE TABLE t (group_id INT, a VARBINARY(6));
mysql&gt; INSERT INTO t VALUES (1, NULL);
mysql&gt; INSERT INTO t VALUES (1, NULL);
mysql&gt; INSERT INTO t VALUES (2, NULL);
mysql&gt; INSERT INTO t VALUES (2, X'1234');
mysql&gt; INSERT INTO t VALUES (2, X'FF34');
mysql&gt; SELECT HEX(BIT_AND(a)), HEX(BIT_OR(a)), HEX(BIT_XOR(a))
       FROM t GROUP BY group_id;
+-----------------+----------------+-----------------+
| HEX(BIT_AND(a)) | HEX(BIT_OR(a)) | HEX(BIT_XOR(a)) |
+-----------------+----------------+-----------------+
| FFFFFFFFFFFF    | 000000000000   | 000000000000    |
| 1234            | FF34           | ED00            |
+-----------------+----------------+-----------------+</code></pre>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="bit-operations-literal-handling"></a>Special Handling of Hexadecimal Literals, Bit Literals, and NULL
Literals</h3>
</div>
</div>
</div>
<p>
        For backward compatibility, MySQL 8.0 evaluates bit operations
        in numeric context when all bit arguments are hexadecimal
        literals, bit literals, or <code class="literal">NULL</code> literals.
        That is, bit operations on binary-string bit arguments do not
        use binary-string evaluation if all bit arguments are unadorned
        hexadecimal literals, bit literals, or <code class="literal">NULL</code>
        literals. (This does not apply to such literals if they are
        written with a <code class="literal">_binary</code> introducer,
        <a class="link" href="cast-functions.html#operator_binary"><code class="literal">BINARY</code></a> operator, or other way of
        specifying them explicitly as binary strings.)
      </p><p>
        The literal handling just described is the same as prior to
        MySQL 8.0. Examples:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            These bit operations evaluate the literals in numeric
            context and produce a <code class="literal">BIGINT</code> result:
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">b'0001' | b'0010'
X'0008' &lt;&lt; 8</code></pre></li><li class="listitem"><p>
            These bit operations evaluate <code class="literal">NULL</code> in
            numeric context and produce a <code class="literal">BIGINT</code>
            result that has a <code class="literal">NULL</code> value:
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">NULL &amp; NULL
NULL &gt;&gt; 4</code></pre></li></ul>
</div>
<p>
        In MySQL 8.0, you can cause those operations to evaluate the
        arguments in binary-string context by indicating explicitly that
        at least one argument is a binary string:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">_binary b'0001' | b'0010'
_binary X'0008' &lt;&lt; 8
BINARY NULL &amp; NULL
BINARY NULL &gt;&gt; 4</code></pre><p>
        The result of the last two expressions is
        <code class="literal">NULL</code>, just as without the
        <code class="literal">BINARY</code> operator, but the data type of the
        result is a binary string type rather than an integer type.
</p>
</div>
<div class="simplesect">
<div class="titlepage">
<div>
<div class="simple">
<h3 class="title"><a name="bit-operations-incompatibilities"></a>Bit-Operation Incompatibilities with MySQL 5.7</h3>

</div>

</div>

</div>
<p>
        Because bit operations can handle binary string arguments
        natively in MySQL 8.0, some expressions produce a different
        result in MySQL 8.0 than in 5.7. The five problematic expression
        types to watch out for are:
      </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql"><em class="replaceable">nonliteral_binary</em> { &amp; | ^ } <em class="replaceable">binary</em>
<em class="replaceable">binary</em>  { &amp; | ^ } <em class="replaceable">nonliteral_binary</em>
<em class="replaceable">nonliteral_binary</em> { &lt;&lt; &gt;&gt; } <em class="replaceable">anything</em>
~ <em class="replaceable">nonliteral_binary</em>
<em class="replaceable">AGGR_BIT_FUNC</em>(<em class="replaceable">nonliteral_binary</em>)</code></pre><p>
        Those expressions return <a class="link" href="integer-types.html" title="11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT"><code class="literal">BIGINT</code></a>
        in MySQL 5.7, binary string in 8.0.
      </p><p>
        Explanation of notation:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
            <code class="literal">{ <em class="replaceable"><code>op1</code></em>
            <em class="replaceable"><code>op2</code></em> ... }</code>: List of
            operators that apply to the given expression type.
          </p></li><li class="listitem"><p>
            <em class="replaceable"><code>binary</code></em>: Any kind of binary string
            argument, including a hexadecimal literal, bit literal, or
            <code class="literal">NULL</code> literal.
          </p></li><li class="listitem"><p>
            <em class="replaceable"><code>nonliteral_binary</code></em>: An argument
            that is a binary string value other than a hexadecimal
            literal, bit literal, or <code class="literal">NULL</code> literal.
          </p></li><li class="listitem"><p>
            <em class="replaceable"><code>AGGR_BIT_FUNC</code></em>: An aggregate
            function that takes bit-value arguments:
            <a class="link" href="group-by-functions.html#function_bit-and"><code class="literal">BIT_AND()</code></a>,
            <a class="link" href="group-by-functions.html#function_bit-or"><code class="literal">BIT_OR()</code></a>,
            <a class="link" href="group-by-functions.html#function_bit-xor"><code class="literal">BIT_XOR()</code></a>.
</p></li></ul>
</div>
<p>
        For information about how to prepare in MySQL 5.7 for potential
        incompatibilities between MySQL 5.7 and 8.0, see
        <a class="ulink" href="https://dev.mysql.com/doc/refman/5.7/en/bit-functions.html" target="_top">Bit Functions and Operators</a>, in
        <a class="ulink" href="https://dev.mysql.com/doc/refman/5.7/en/" target="_top">MySQL 5.7 Reference Manual</a>.
      </p><p>
        The following list describes available bit functions and
        operators:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><a name="operator_bitwise-or"></a><p>
            <a class="indexterm" name="idm139663188319392"></a>

            <a class="indexterm" name="idm139663188318320"></a>

            <a class="link" href="bit-functions.html#operator_bitwise-or"><code class="literal">|</code></a>
          </p><p>
            Bitwise OR.
          </p><p>
            The result type depends on whether the arguments are
            evaluated as binary strings or numbers:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                Binary-string evaluation occurs when the arguments have
                a binary string type, and at least one of them is not a
                hexadecimal literal, bit literal, or
                <code class="literal">NULL</code> literal. Numeric evaluation
                occurs otherwise, with argument conversion to unsigned
                64-bit integers as necessary.
              </p></li><li class="listitem"><p>
                Binary-string evaluation produces a binary string of the
                same length as the arguments. If the arguments have
                unequal lengths, an
                <a class="link" href="server-error-reference.html#error_er_invalid_bitwise_operands_size"><code class="literal">ER_INVALID_BITWISE_OPERANDS_SIZE</code></a>
                error occurs. Numeric evaluation produces an unsigned
                64-bit integer.
</p></li></ul>
</div>
<p>
            For more information, see the introductory discussion in
            this section.
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT 29 | 15;
        -&gt; 31
mysql&gt; SELECT _binary X'40404040' | X'01020304';
        -&gt; 'ABCD'</code></pre></li><li class="listitem"><a name="operator_bitwise-and"></a><p>
            <a class="indexterm" name="idm139663188302592"></a>

            <a class="indexterm" name="idm139663188301504"></a>

            <a class="link" href="bit-functions.html#operator_bitwise-and"><code class="literal">&amp;</code></a>
          </p><p>
            Bitwise AND.
          </p><p>
            The result type depends on whether the arguments are
            evaluated as binary strings or numbers:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                Binary-string evaluation occurs when the arguments have
                a binary string type, and at least one of them is not a
                hexadecimal literal, bit literal, or
                <code class="literal">NULL</code> literal. Numeric evaluation
                occurs otherwise, with argument conversion to unsigned
                64-bit integers as necessary.
              </p></li><li class="listitem"><p>
                Binary-string evaluation produces a binary string of the
                same length as the arguments. If the arguments have
                unequal lengths, an
                <a class="link" href="server-error-reference.html#error_er_invalid_bitwise_operands_size"><code class="literal">ER_INVALID_BITWISE_OPERANDS_SIZE</code></a>
                error occurs. Numeric evaluation produces an unsigned
                64-bit integer.
</p></li></ul>
</div>
<p>
            For more information, see the introductory discussion in
            this section.
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT 29 &amp; 15;
        -&gt; 13
mysql&gt; SELECT HEX(_binary X'FF' &amp; b'11110000');
        -&gt; 'F0'</code></pre></li><li class="listitem"><a name="operator_bitwise-xor"></a><p>
            <a class="indexterm" name="idm139663188285840"></a>

            <a class="indexterm" name="idm139663188284768"></a>

            <a class="link" href="bit-functions.html#operator_bitwise-xor"><code class="literal">^</code></a>
          </p><p>
            Bitwise XOR.
          </p><p>
            The result type depends on whether the arguments are
            evaluated as binary strings or numbers:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                Binary-string evaluation occurs when the arguments have
                a binary string type, and at least one of them is not a
                hexadecimal literal, bit literal, or
                <code class="literal">NULL</code> literal. Numeric evaluation
                occurs otherwise, with argument conversion to unsigned
                64-bit integers as necessary.
              </p></li><li class="listitem"><p>
                Binary-string evaluation produces a binary string of the
                same length as the arguments. If the arguments have
                unequal lengths, an
                <a class="link" href="server-error-reference.html#error_er_invalid_bitwise_operands_size"><code class="literal">ER_INVALID_BITWISE_OPERANDS_SIZE</code></a>
                error occurs. Numeric evaluation produces an unsigned
                64-bit integer.
</p></li></ul>
</div>
<p>
            For more information, see the introductory discussion in
            this section.
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT 1 ^ 1;
        -&gt; 0
mysql&gt; SELECT 1 ^ 0;
        -&gt; 1
mysql&gt; SELECT 11 ^ 3;
        -&gt; 8
mysql&gt; SELECT HEX(_binary X'FEDC' ^ X'1111');
        -&gt; 'EFCD'</code></pre></li><li class="listitem"><a name="operator_left-shift"></a><p>
            <a class="indexterm" name="idm139663188267616"></a>

            <a class="link" href="bit-functions.html#operator_left-shift"><code class="literal">&lt;&lt;</code></a>
          </p><p>
            Shifts a longlong (<a class="link" href="integer-types.html" title="11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT"><code class="literal">BIGINT</code></a>)
            number or binary string to the left.
          </p><p>
            The result type depends on whether the bit argument is
            evaluated as a binary string or number:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                Binary-string evaluation occurs when the bit argument
                has a binary string type, and is not a hexadecimal
                literal, bit literal, or <code class="literal">NULL</code>
                literal. Numeric evaluation occurs otherwise, with
                argument conversion to an unsigned 64-bit integer as
                necessary.
              </p></li><li class="listitem"><p>
                Binary-string evaluation produces a binary string of the
                same length as the bit argument. Numeric evaluation
                produces an unsigned 64-bit integer.
</p></li></ul>
</div>
<p>
            Bits shifted off the end of the value are lost without
            warning, regardless of the argument type. In particular, if
            the shift count is greater or equal to the number of bits in
            the bit argument, all bits in the result are 0.
          </p><p>
            For more information, see the introductory discussion in
            this section.
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT 1 &lt;&lt; 2;
        -&gt; 4
mysql&gt; SELECT HEX(_binary X'00FF00FF00FF' &lt;&lt; 8);
        -&gt; 'FF00FF00FF00'</code></pre></li><li class="listitem"><a name="operator_right-shift"></a><p>
            <a class="indexterm" name="idm139663188251536"></a>

            <a class="link" href="bit-functions.html#operator_right-shift"><code class="literal">&gt;&gt;</code></a>
          </p><p>
            Shifts a longlong (<a class="link" href="integer-types.html" title="11.2.1 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT"><code class="literal">BIGINT</code></a>)
            number or binary string to the right.
          </p><p>
            The result type depends on whether the bit argument is
            evaluated as a binary string or number:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                Binary-string evaluation occurs when the bit argument
                has a binary string type, and is not a hexadecimal
                literal, bit literal, or <code class="literal">NULL</code>
                literal. Numeric evaluation occurs otherwise, with
                argument conversion to an unsigned 64-bit integer as
                necessary.
              </p></li><li class="listitem"><p>
                Binary-string evaluation produces a binary string of the
                same length as the bit argument. Numeric evaluation
                produces an unsigned 64-bit integer.
</p></li></ul>
</div>
<p>
            Bits shifted off the end of the value are lost without
            warning, regardless of the argument type. In particular, if
            the shift count is greater or equal to the number of bits in
            the bit argument, all bits in the result are 0.
          </p><p>
            For more information, see the introductory discussion in
            this section.
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT 4 &gt;&gt; 2;
        -&gt; 1
mysql&gt; SELECT HEX(_binary X'00FF00FF00FF' &gt;&gt; 8);
        -&gt; '0000FF00FF00'</code></pre></li><li class="listitem"><a name="operator_bitwise-invert"></a><p>
            <a class="indexterm" name="idm139663188235552"></a>

            <a class="link" href="bit-functions.html#operator_bitwise-invert"><code class="literal">~</code></a>
          </p><p>
            Invert all bits.
          </p><p>
            The result type depends on whether the bit argument is
            evaluated as a binary string or number:
</p>
<div class="itemizedlist">
<ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
                Binary-string evaluation occurs when the bit argument
                has a binary string type, and is not a hexadecimal
                literal, bit literal, or <code class="literal">NULL</code>
                literal. Numeric evaluation occurs otherwise, with
                argument conversion to an unsigned 64-bit integer as
                necessary.
              </p></li><li class="listitem"><p>
                Binary-string evaluation produces a binary string of the
                same length as the bit argument. Numeric evaluation
                produces an unsigned 64-bit integer.
</p></li></ul>
</div>
<p>
            For more information, see the introductory discussion in
            this section.
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT 5 &amp; ~1;
        -&gt; 4
mysql&gt; SELECT HEX(~X'0000FFFF1111EEEE');
        -&gt; 'FFFF0000EEEE1111'</code></pre></li><li class="listitem"><a name="function_bit-count"></a><p>
            <a class="indexterm" name="idm139663188221584"></a>

            <a class="link" href="bit-functions.html#function_bit-count"><code class="literal">BIT_COUNT(<em class="replaceable"><code>N</code></em>)</code></a>
          </p><p>
            Returns the number of bits that are set in the argument
            <em class="replaceable"><code>N</code></em> as an unsigned 64-bit integer,
            or <code class="literal">NULL</code> if the argument is
            <code class="literal">NULL</code>.
          </p><pre class="programlisting copytoclipboard line-numbers language-sql"><code class="language-sql">mysql&gt; SELECT BIT_COUNT(64), BIT_COUNT(BINARY 64);
        -&gt; 1, 7
mysql&gt; SELECT BIT_COUNT('64'), BIT_COUNT(_binary '64');
        -&gt; 1, 7
mysql&gt; SELECT BIT_COUNT(X'40'), BIT_COUNT(_binary X'40');
        -&gt; 1, 1</code></pre></li></ul>
</div>

</div>

</div>

<br />
        </div>

                <div id="docs-in-page-nav-container">
            <div id="docs-in-page-nav">
                
    <a href="xml-functions.html"
        aria-label="Previous" title="Previous: XML Functions"><span
        class="icon-chevron-left"></span> PREV</a> &nbsp;
<a href="index.html" aria-label="Start" title="Start"> HOME</a> &nbsp;
        <a aria-label="Up" href="functions.html" title="Up: Functions and Operators"> UP</a> &nbsp;
    <a href="encryption-functions.html" aria-label="Next"
        title="Next: Encryption and Compression Functions">NEXT <span
        class="icon-chevron-right"></span></a>
            </div>
        </div>
        
         <div id="docs-body-extra">
             
<a class="docs-sidebar-section" href=""><span class="icon-related"></span>
    <span class="text">Related Documentation</span></a>
<div class="docs-sidebar-accordian">
    <div class="text">
        <a href="/doc/relnotes/mysql/8.0/en/">MySQL 8.0 Release Notes</a><br />
        <a href="/doc/dev/mysql-server/latest/">MySQL 8.0 Source Code Documentation</a><br />
        </div>
</div>

    <a class="docs-sidebar-section" href=""><span class="icon-download-thin"></span>
        <span class="text">
            Download
                            this Manual
                    </span>
    </a>
    <div class="docs-sidebar-accordian">
        <div class="text">
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.pdf">PDF (US Ltr)</a>
            - 46.1Mb<br />
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.a4.pdf">PDF (A4)</a>
            - 46.1Mb<br />
                        <a href="https://downloads.mysql.com/docs/mysql-ref-manual-8.0-en-pdf-1-20190611.noarch.rpm">PDF (RPM)</a>
            - 41.5Mb<br />
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.html-chapter.tar.gz">HTML Download (TGZ)</a>
            - 10.6Mb<br />
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.html-chapter.zip">HTML Download (Zip)</a>
            - 10.6Mb<br />
                        <a href="https://downloads.mysql.com/docs/mysql-ref-manual-8.0-en-html-chapter-1-20190611.noarch.rpm">HTML Download (RPM)</a>
            - 9.1Mb<br />
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.man-gpl.tar.gz">Man Pages (TGZ)</a>
            - 220.4Kb<br />
                        <a href="https://downloads.mysql.com/docs/refman-8.0-en.man-gpl.zip">Man Pages (Zip)</a>
            - 325.8Kb<br />
                        <a href="https://downloads.mysql.com/docs/mysql-8.0.info.gz">Info (Gzip)</a>
            - 4.1Mb<br />
                        <a href="https://downloads.mysql.com/docs/mysql-8.0.info.zip">Info (Zip)</a>
            - 4.1Mb<br />
                    </div>
    </div>

<a class="docs-sidebar-section" href=""><span class="icon-book"></span>
    <span class="text">Excerpts from this Manual</span></a>
<div class="docs-sidebar-accordian">
    <div class="text">
                <a href="/doc/mysql-backup-excerpt/8.0/en/">MySQL Backup and Recovery</a><br />
                <a href="/doc/mysql-g11n-excerpt/8.0/en/">MySQL Globalization</a><br />
                <a href="/doc/mysql-infoschema-excerpt/8.0/en/">MySQL Information Schema</a><br />
                <a href="/doc/mysql-installation-excerpt/8.0/en/">MySQL Installation Guide</a><br />
                <a href="/doc/mysql-security-excerpt/8.0/en/">Security in MySQL</a><br />
                <a href="/doc/mysql-startstop-excerpt/8.0/en/">Starting and Stopping MySQL</a><br />
                <a href="/doc/mysql-linuxunix-excerpt/8.0/en/">MySQL and Linux/Unix</a><br />
                <a href="/doc/mysql-windows-excerpt/8.0/en/">MySQL and Windows</a><br />
                <a href="/doc/mysql-osx-excerpt/8.0/en/">MySQL and OS X</a><br />
                <a href="/doc/mysql-solaris-excerpt/8.0/en/">MySQL and Solaris</a><br />
                <a href="/doc/mysql-sourcebuild-excerpt/8.0/en/">Building MySQL from Source</a><br />
                <a href="/doc/mysql-reslimits-excerpt/8.0/en/">MySQL Restrictions and Limitations</a><br />
                <a href="/doc/mysql-partitioning-excerpt/8.0/en/">MySQL Partitioning</a><br />
                <a href="/doc/mysql-secure-deployment-guide/8.0/en/">MySQL Secure Deployment Guide</a><br />
                <a href="/doc/mysql-tutorial-excerpt/8.0/en/">MySQL Tutorial</a><br />
                <a href="/doc/mysql-perfschema-excerpt/8.0/en/">MySQL Performance Schema</a><br />
                <a href="/doc/mysql-replication-excerpt/8.0/en/">MySQL Replication</a><br />
                <a href="/doc/mysql-repo-excerpt/8.0/en/">Using the MySQL Yum Repository</a><br />
            </div>
</div>
         </div>

              </div>
     </div>

</div>

<script>
$(function() {
    var doc = new $.doc({ 'mobile': $.browser.mobile, 'docId': 1, 'highlight': true });
});
</script>
    

            
        </div>
    </div>

    <footer class="collapsed">
        <div id="footer-bottom">
                                    <div id="footer-collapse">
                <a href="" id="expand-footer"
                    aria-label="Expand Footer"
                    title="Expand Footer"><span
                    class="icon-plus-square"></span></a>
            </div>
                        <div class="footer-contact">
                <div id="footer-contact-icon" style="display: none;">
                    <span class="icon-call-phone"></span>
                </div>
                <div id="footer-contact-numbers" style="display: none;">
                    <strong>Contact MySQL Sales</strong><br/>
                    USA/Canada: +1-866-221-0634 &nbsp;
                    (<a href="https://www.mysql.com/about/contact/phone/">More Countries &raquo;</a>)
                </div>
                <div id="footer-contact-copyright" style="display: inline-block;">
                    <a href="http://www.oracle.com/" aria-label="Oracle" title="Oracle"><span class="oracle-logo"></span></a>
                    &nbsp; &copy; 2019, Oracle Corporation and/or its affiliates
                </div>
            </div>
            <div class="social-icons">
                <a aria-label="Join us on Facebook" title="Join us on Facebook" href="http://www.facebook.com/mysql"><span class="icon-facebook-square"></span></a>
                <a aria-label="Follow us on Twitter" title="Follow us on Twitter" href="https://twitter.com/mysql"><span class="icon-twitter-square"></span></a>
                <a aria-label="Follow us on LinkedIn" title="Follow us on LinkedIn" href="https://www.linkedin.com/company/mysql"><span class="icon-linkedin-square"></span></a>
                <a aria-label="Visit our YouTube channel" title="Visit our YouTube channel" href="http://www.youtube.com/mysqlchannel"><span class="icon-youtube-square"></span></a>
            </div>
            <br class="clear" />
        </div>
        <div id="footer-links">

            <div id="footer-nav"></div>

            <div class="links">
                <ul>
                    <li class="top"><a href="https://www.mysql.com/products/">Products</a></li>
                    <li><a href="https://www.mysql.com/cloud/">Oracle MySQL Cloud Service</a></li>
                    <li><a href="https://www.mysql.com/products/enterprise/">MySQL Enterprise Edition</a></li>
                    <li><a href="https://www.mysql.com/products/standard/">MySQL Standard Edition</a></li>
                    <li><a href="https://www.mysql.com/products/classic/">MySQL Classic Edition</a></li>
                    <li><a href="https://www.mysql.com/products/cluster/">MySQL Cluster CGE</a></li>
                    <li><a href="https://www.mysql.com/oem/">MySQL Embedded (OEM/ISV)</a></li>
                </ul>
            </div>

            <div class="links">
                <ul>
                    <li class="top"><a href="https://www.mysql.com/services/">Services</a></li>
                    <li><a href="https://www.mysql.com/training/">Training</a></li>
                    <li><a href="https://www.mysql.com/certification/">Certification</a></li>
                    <li><a href="https://www.mysql.com/consulting/">Consulting</a></li>
                    <li><a href="https://www.mysql.com/support/">Support</a></li>
                </ul>
            </div>

            <div class="links">
                <ul>
                    <li class="top"><a href="https://dev.mysql.com/downloads/">Downloads</a></li>
                    <li><a href="https://dev.mysql.com/downloads/mysql/">MySQL Community Server</a></li>
                    <li><a href="https://dev.mysql.com/downloads/cluster/">MySQL NDB Cluster</a></li>
                    <li><a href="https://dev.mysql.com/downloads/shell/">MySQL Shell</a></li>
                    <li><a href="https://dev.mysql.com/downloads/router/">MySQL Router</a></li>
                    <li><a href="https://dev.mysql.com/downloads/workbench/">MySQL Workbench</a></li>
                </ul>
            </div>

            <div class="links">
                <ul>
                    <li class="top"><a href="https://dev.mysql.com/doc/">Documentation</a></li>
                    <li><a href="https://dev.mysql.com/doc/refman/en/">MySQL Reference Manual</a></li>
                    <li><a href="https://dev.mysql.com/doc/workbench/en/">MySQL Workbench</a></li>
                    <li><a href="https://dev.mysql.com/doc/index-cluster.html">MySQL NDB Cluster</a></li>
                    <li><a href="https://dev.mysql.com/doc/index-connectors.html">MySQL Connectors</a></li>
                    <li><a href="https://dev.mysql.com/doc/#topic">Topic Guides</a></li>
                </ul>
            </div>

            <div class="links">
                <ul>
                    <li class="top"><a href="https://www.mysql.com/about/">About MySQL</a></li>
                    <li><a href="https://www.mysql.com/about/contact/">Contact Us</a></li>
                                                            <li><a href="https://www.mysql.com/buy-mysql/">How to Buy</a></li>
                    <li><a href="https://www.mysql.com/partners/">Partners</a></li>
                    <li><a href="https://www.mysql.com/about/jobs/">Job Opportunities</a></li>
                    <li><a href="https://www.mysql.com/sitemap.html">Site Map</a></li>
                </ul>
            </div>

            
            <div id="footer-logo">
                <a href="http://www.oracle.com/" aria-label="Oracle" title="Oracle"><span class="oracle-logo"></span></a>
                &nbsp; &copy; 2019, Oracle Corporation and/or its affiliates
                <div class="footer-legal-links">
                    <a href="https://www.mysql.com/about/legal/">Legal Policies</a> |
                    <a href="http://www.oracle.com/us/legal/privacy/index.htm">Your Privacy Rights</a> |
                    <a href="http://www.oracle.com/us/legal/terms/index.html">Terms of Use</a> |
                    <a href="http://www.oracle.com/us/legal/third-party-trademarks/index.html">Trademark Policy</a> |
                    <a href="http://www.oracle.com/technetwork/community/oca-486395.html">Contributor Agreement</a> |
                    <div id="teconsent" style="display: inline-block"><script async="async" type="text/javascript" src="js/notice.js" crossorigin=""></script></div>
                </div>
            </div>

        </div>
    </footer>
</div>

        <script src="js/s_code_remote.js"></script>

        
    
</body>
</html>
